﻿using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;

namespace EPlatform_web.App_Start
{
    public class DbObject
    {
        /// <summary>
        /// 域，[web.config]
        /// </summary>
        private static readonly string Contract = ConfigurationManager.AppSettings["Contract"];
        protected OracleConnection Connection;
        protected string ConnectionString
        {
            get { return ConnectionString; }
        }
        public DbObject(string sDbName, string sConnUid, string sConnPwd)
        {
            Connection = new OracleConnection("data source=" + sDbName + ";user id=" + sConnUid + ";password=" + sConnPwd + ";");
        }
        public DataSet GetDataSet(string querySql)
        {
            try
            {
                Connection.Open();
                OracleDataAdapter mycommand = new OracleDataAdapter(querySql, Connection);
                DataSet ds = new DataSet();
                mycommand.Fill(ds);
                Connection.Close();
                return ds;
            }
            catch (Exception)
            {
                Connection.Close();
                throw;
            }
        }
        public int RunSql(string querySql)
        {
            try
            {
                int i;
                Connection.Open();
                OracleCommand command = new OracleCommand(querySql, Connection);
                i = command.ExecuteNonQuery();
                Connection.Close();
                return i;
            }
            catch (Exception)
            {
                Connection.Close();
                throw;
            }
        }

        public OracleCommand BuildIntCommand(string storedProcName, IDataParameter[] parameters)
        {
            try
            {
                OracleCommand command = BuildQueryCommand(storedProcName, parameters);
                return command;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public OracleCommand BuildQueryCommand(string storedProcName, IDataParameter[] parameters)
        {
            try
            {
                OracleCommand command = new OracleCommand(storedProcName, Connection);
                command.CommandType = CommandType.StoredProcedure;

                foreach (OracleParameter parameter in parameters)
                {
                    if (parameter.Value == null)
                    {
                        parameter.Value = DBNull.Value;
                    }
                    command.Parameters.Add(parameter);
                }
                return command;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public int RunProcedure(string storedProcName, IDataParameter[] parameters)
        {
            try
            {
                int result;
                Connection.Open();
                OracleCommand command = BuildIntCommand(storedProcName, parameters);
                result = command.ExecuteNonQuery();
                Connection.Close();
                return result;
            }
            catch (Exception)
            {
                Connection.Close();
                throw;
            }
        }
        public string RunProcedureout(string storedProcName, IDataParameter[] parameters)
        {
            string result;
            string area_flag_ = (string)parameters[0].Value;
            string attr_ = (string)parameters[1].Value;
            try
            {
                Connection.Open();
                OracleCommand command = BuildIntCommand(storedProcName, parameters);
                command.ExecuteNonQuery();
                if (command.Parameters["result_"].Value is DBNull)
                {
                    result = "NULL";
                    Connection.Close();
                }
                else
                {
                    result = command.Parameters["result_"].Value.ToString();
                    Connection.Close();
                }
                return result;
            }
            catch (Exception e)
            {
                string nowtime = DateTime.Now.ToString("yyyy-MM-dd") + " " + DateTime.Now.ToString("hh-mm-ss");
                CreateTxtFile(nowtime);
                CreateTxtFile(area_flag_);
                CreateTxtFile(attr_);
                CreateTxtFile(e.ToString());

                result = "SQLNULL";
                Connection.Close();
                return result;
            }
        }
        public void CreateTxtFile(string strMessage)
        {
            try
            {
                string fileName = DateTime.Now.ToString("yyyy-MM-dd");
                string text = strMessage + "\r\n";

                string filename;
                Byte[] b;
                b = System.Text.Encoding.Default.GetBytes(text);
                filename = "c:\\inetpub\\wwwroot\\syslog\\" + fileName + ".txt";
                System.IO.FileStream fs;
                if (System.IO.File.Exists(fileName))
                {
                    fs = new System.IO.FileStream(filename, System.IO.FileMode.Create);
                    fs.Write(b, 0, b.Length);
                    fs.Close();
                }
                else
                {
                    fs = new System.IO.FileStream(filename, System.IO.FileMode.Append);
                    fs.Write(b, 0, b.Length);
                    fs.Close();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}